<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2023/10/13
 * Time: 10:37 AM
 */

namespace app\zyhsapi\model;

use think\Config;
use think\Log;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsUserAccountRecord extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_user_account_record';
    protected $cacheModel = 'user.coin_detail';

    /**
     * 添加用户准币明细
     * @param $uid
     * @param $data
     * @return false|int|string
     * @throws \Exception
     */
    public function SaveData($uid, $data)
    {
        try
        {
            $id = $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            Log::write($uid . '添加用户准币明细失败：' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】');
            return false;
        }
        CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache('list');
        return $id;
    }

    /**
     * 更新用户钱包明细数据
     * @param int $uid
     * @param int $id
     * @param array $data
     * @param array $where
     * @return bool
     * @throws Exception
     */
    public function UpdateData($uid, $id, $data, $where)
    {
        try
        {
            $this->where($where)->update($data);
        }
        catch (\Exception $e)
        {
            Log::write($uid . '更新用户准币明细失败：' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】');
            return false;
        }
        CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $uid)->rmCache('list');
        CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        return true;
    }

    /**
     * 缓存获取用户准币明细
     * @param $uid
     * @param $isPaging
     * @return array|mixed
     * @throws \app\common\lib\exception\ApiException
     */
    public function getUserCoinDetailListFromCache($uid, $isPaging = true)
    {
        $page = input('page', 1);
        $limit = $this->pageLimit ?: Config::get('paginate.list_rows');

        if (!$isPaging)
        {
            $page = -1; // 获取所有数据
        }

        $sortKey = 'id';

        $data = CacheManageZyhs::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setPage($page)
            ->setPageSize($limit)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($uid)
            {
                return $this->getInfoByIdsInData($ids, [$uid], $sortType);
            }, $sortKey, [], 0, 'list', 'list_details');

        if (!$isPaging)
        {
            return $data['data'];
        }

        return $data;
    }

    /**
     * 数据库批量获取用户准币明细
     * @param array $ids
     * @param array $type
     * @param string $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        $field = 'id,uid,consumer_type,order_type,gid,pray_date,total_amount,coin_num,balance,order_id,trade_type,create_time,update_time,extend';
        if ($ids)
        {
            $where = ['id' => ['in', $ids], 'status' => 1];
        } else
        {
            $where = ['uid' => $type[0], 'status' => 1];
        }
        try
        {
            $data = $this->where($where)->order('id', 'DESC')->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}
